package com.lxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.common.R;
import com.lxp.dto.SetmealDto;
import com.lxp.entity.Category;
import com.lxp.entity.Setmeal;
import com.lxp.service.CategoryService;
import com.lxp.service.SetmealDishService;
import com.lxp.service.SetmealService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lxp
 * @Motto 每天一行小代码，健康幸福学JAVA：
 * @create 2022-08-26 18:52
 * @Description:
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐信息
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveWithDish(setmealDto);
        return R.success("套餐信息添加成功");
    }

    /**
     * 套餐的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<IPage> page(Integer page, Integer pageSize, String name) {


        // 构造分页查询
        IPage<Setmeal> iPage = new Page<>(page, pageSize);
        IPage<SetmealDto> dtoIPage = new Page<>();


        // 构造查询条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        // 添加查询条件, 根据name进行like 模糊查询
        lqw.like(Strings.isNotEmpty(name), Setmeal::getName, name);
        // 排序条件
        lqw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(iPage, lqw);

        // 进行对象拷贝
        BeanUtils.copyProperties(iPage, dtoIPage, "records");
        List<Setmeal> records = iPage.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(item, setmealDto);
            // 分类ID
            Long categoryId = item.getCategoryId();
            // 查询分类的名称
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                // 分类的名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);

            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoIPage.setRecords(list);

        return R.success(dtoIPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

}

